package com.ke.jdyd.presenter;

import android.util.Log;

import com.google.gson.JsonObject;
import com.ke.jdyd.http.BaseResponse;
import com.ke.jdyd.http.HttpResultObservable;
import com.ke.jdyd.model.GoodsEntity;
import com.ke.jdyd.model.GoodsInfo;
import com.ke.jdyd.service.HomeSerivce;
import com.ke.jdyd.service.LoginService;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class HomePresenter {
  private HomeView mView;
  private HomeSerivce homeSerivce;

  public HomePresenter(HomeView view) {
    this.mView = view;
    homeSerivce = new HomeSerivce();
  }

  public void loadData() {
    homeSerivce.getGoodsList().subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new HttpResultObservable<List<GoodsInfo>>() {
          @Override
          public void onSuccess(List<GoodsInfo> result) {
            mView.loadSuccess(result);

          }

          @Override
          public void onSuccess(List<GoodsInfo> result, int code, String msg) {
            mView.loadFailed(msg);//业务逻辑上的加载失败
          }

          @Override
          public void onFailed(String msg) {
            mView.loadFailed(msg);//网络加载异常

          }
        });

  }
  public void getGoodsDetail(String id) {
    homeSerivce.getGoodsDetail(id).subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new HttpResultObservable<GoodsEntity>() {
            @Override
            public void onSuccess(GoodsEntity result) {
                Log.d("TAG", "onSuccess: result" +result.toString());

            }

            @Override
            public void onSuccess(GoodsEntity result, int code, String msg) {

            }

            @Override
            public void onFailed(String msg) {

            }
        });

  }

  public interface HomeView {//定义请求成功和失败的接口
    void loadSuccess(List<GoodsInfo> result);

    void loadFailed(String msg);
  }
}
